package com.wzxy.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wzxy.model.Student;
import com.wzxy.service.IStudentService;

@Controller
@RequestMapping("/student")
public class StudentController {
	@Resource
	private IStudentService studentService;
	
	/**
	 * 跳转到主页
	 */
	@RequestMapping(value = "/indexJSP")
	public String indexJSP(Model model){
		List<Student> list = studentService.queryAllStudents();
		model.addAttribute("list", list);
		return "index";
	}
	
	/**
	 * 接收页面穿来的学生对象
	 * 保存学生对象
	 */
	@RequestMapping(value = "/add",method = RequestMethod.POST)
	public String addStudnt(Student s,Model model){
		studentService.addStudent(s);
		return "redirect:/student/indexJSP"; //重定向，重新调用跳到主页的方法
	}
	
	
	@RequestMapping(value = "/delete",method = RequestMethod.GET)
	public String deleteStudent(String sid,Model model){
		studentService.deleteStudent(sid);
		return "redirect:/student/indexJSP";
	}
	
	/*
	 * 从主页跳转到更新的页面，并且向页面传递参数
	 */
	@RequestMapping(value = "updateJSP", method = RequestMethod.GET)
	public String updateJSP(String sid,Model model){
		Student student = studentService.queryStudentById(sid);
		model.addAttribute("sid", student.getSid());
		model.addAttribute("sname",student.getSname());
		return "update";
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(Student s){
		studentService.updateStudent(s);
		return "redirect:/student/indexJSP"; 
	}
}
